<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>联调工具</title>

<link rel="stylesheet" type="text/css" href="<%=path%>/easyui/themes/gray/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=path%>/easyui/themes/icon.css">
<script type="text/javascript" src="<%=path%>/easyui/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=path%>/easyui/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=path%>/easyui/js/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="<%=path%>/easyui/js/con_isok.js"></script>

</head>
<body class="easyui-layout">

	<div data-options="region:'north',border:false" style="padding:2px;">
		<table align="">
			<tr>
				<td>油站代码:</td>
				<td><input id="node_no_id" class="easyui-numberbox" data-options="required:true">
				</td>
				<td>IP地址:</td>
				<td><input id="ip_address_id" class="easyui-validatebox"></td>

			</tr>
			<tr>
				<td colspan="2" align="center"><a id="select_btn" href="javascript:void(0);" class="easyui-linkbutton" onclick="select_data1();">查询</a>
				</td>
				<td colspan="2" align="center"><a id="sync_btn" href="javascript:void(0);" class="easyui-linkbutton" onclick="sync1();">上传并同步</a>
				</td>
			</tr>
		</table>
	</div>

	<div data-options="region:'center', border:false" style="padding:2px;">
		<table id="dg" class="easyui-datagrid" data-options="
			url:'<%=path%>/liantiao/index.do',
			fit:true,
			singleSelect:true,
			fitColumns: true,
			border:false
	">
			<thead>
				<tr>
					<th data-options="field:'bfSale',width:100,formatter:formatModel">销售表</th>
					<th data-options="field:'bfStockHoutData',width:100,formatter:formatModel">时点库存</th>
					<th data-options="field:'bfSaleFilter',width:100,formatter:formatModel">销售过滤表</th>
					<th data-options="field:'bfStockHoutDataFilter',width:100,formatter:formatModel">时点库存过滤表</th>
				</tr>
			</thead>
		</table>
	</div>

	<script type="text/javascript">
		
		$(function(){
			$("#sync_btn").linkbutton('disable');
		});
		
		function isRightIP(value) {
			var ip_rex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
			return ip_rex.test(value);
		}
		
		
		function formatModel(val,index,row){
			if(val == 0){
				return val;
			}
			return "<b><font color='red'>"+ val +"</font></b>"
		}
	
		function select_data1(){
			var nodeNo = $("#node_no_id").val();	
			if(!nodeNo){
				$.messager.alert("提示","请输入油站编码!", "error");
				return;
			}
			isOkCon("", "<%=path %>","select_data()");
		}
		
		function select_data(){
			var nodeNo = $("#node_no_id").val();	
			if(!nodeNo){
				$.messager.alert("提示","请输入油站编码!", "error");
				return;
			}
			
			$("#dg").datagrid('load', {
				nodeNo:nodeNo
			});
			$("#sync_btn").linkbutton('enable');
		}
		
		
		function sync1(){
			var nodeNo = $("#node_no_id").val();	
			if(!nodeNo){
				$.messager.alert("提示","请输入油站编码!", "error");
				return;
			}
			
			var ipAddress = $("#ip_address_id").val();
			if(!ipAddress){
				$.messager.alert("提示","请输入IP地址!", "error");
				return;
			}
			if(!isRightIP(ipAddress)){
				$.messager.alert("提示","IP地址错误!", "error");
				return;
			}
			isOkCon(ipAddress, "<%=path %>","sync()");
		}
		
		function sync(){
			var nodeNo = $("#node_no_id").val();	
			if(!nodeNo){
				$.messager.alert("提示","请输入油站编码!", "error");
				return;
			}
			
			var ipAddress = $("#ip_address_id").val();
			if(!ipAddress){
				$.messager.alert("提示","请输入IP地址!", "error");
				return;
			}
			if(!isRightIP(ipAddress)){
				$.messager.alert("提示","IP地址错误!", "error");
				return;
			}
			$("#dg").datagrid("selectRow", 0);
			var row = $("#dg").datagrid("getSelected");
			
			if(row.isableSync){
				$.messager.progress({title: "提示", msg: "正在上传并同步.."});
				$.ajax({
					url: "<%=path%>/liantiao/sync.do",
					type: "post",
					dataType: "json",
					data:{
						nodeNo: nodeNo,
						ipAddress: ipAddress,
						bfSale: row.bfSale,
						bfStockHoutData: row.bfStockHoutData
					},
					success:function(data){
						if(data){
							$.messager.alert("提示","成功!", "info");
						}else{
							$.messager.alert("提示","失败!", "error");
						}
					},
					complete:function(){
				 		$.messager.progress('close');
				 	}
				});
				
			}else{
				$.messager.alert("提示","平台已经存在该条数据,不能同步!", "error");
				return;
			}
		}
		
	</script>
</body>
</html>